<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Role;
use App\Models\Menu;
use App\Models\Department;
use App\Models\Position;
use Illuminate\Support\Facades\Hash;

class InitialDataSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $this->createSystemRoles();
        $this->createSystemMenus();
        $this->createRootDepartment();
        $this->createSystemUser();
    }

    /**
     * 创建系统角色
     */
    private function createSystemRoles(): void
    {
        // 超级管理员角色
        Role::create([
            'name' => '超级管理员',
            'code' => 'super_admin',
            'description' => '系统超级管理员，拥有所有权限',
            'type' => 'system',
            'status' => 'active',
            'sort_order' => 1,
            'permissions' => ['*'], // 所有权限
        ]);

        // 系统管理员角色
        Role::create([
            'name' => '系统管理员',
            'code' => 'admin',
            'description' => '系统管理员，负责系统配置和用户管理',
            'type' => 'system',
            'status' => 'active',
            'sort_order' => 2,
            'permissions' => [
                'user.manage',
                'role.manage',
                'menu.manage',
                'department.manage',
                'position.manage',
                'system.log',
                'system.config',
            ],
        ]);

        // HR管理员角色
        Role::create([
            'name' => 'HR管理员',
            'code' => 'hr_admin',
            'description' => 'HR管理员，负责员工管理和人事事务',
            'type' => 'custom',
            'status' => 'active',
            'sort_order' => 3,
            'permissions' => [
                'employee.manage',
                'department.manage',
                'position.manage',
                'contract.manage',
                'attendance.manage',
                'salary.manage',
                'insurance.manage',
            ],
        ]);

        // 部门经理角色
        Role::create([
            'name' => '部门经理',
            'code' => 'department_manager',
            'description' => '部门经理，负责本部门员工管理',
            'type' => 'custom',
            'status' => 'active',
            'sort_order' => 4,
            'permissions' => [
                'employee.view',
                'employee.edit',
                'attendance.manage',
                'approval.manage',
            ],
        ]);

        // 普通员工角色
        Role::create([
            'name' => '普通员工',
            'code' => 'employee',
            'description' => '普通员工，基础权限',
            'type' => 'custom',
            'status' => 'active',
            'sort_order' => 5,
            'permissions' => [
                'profile.view',
                'profile.edit',
                'attendance.view',
                'attendance.clock_in',
            ],
        ]);
    }

    /**
     * 创建系统菜单
     */
    private function createSystemMenus(): void
    {
        // 系统管理菜单
        $systemMenu = Menu::create([
            'name' => '系统管理',
            'code' => 'system',
            'icon' => 'settings',
            'path' => '/system',
            'component' => 'SystemLayout',
            'type' => 'menu',
            'description' => '系统管理相关功能',
            'permissions' => ['system.manage'],
            'status' => 'active',
            'is_visible' => true,
            'sort_order' => 1,
        ]);

        // 用户管理子菜单
        Menu::create([
            'parent_id' => $systemMenu->id,
            'name' => '用户管理',
            'code' => 'user_manage',
            'icon' => 'users',
            'path' => '/system/users',
            'component' => 'UserManage',
            'type' => 'page',
            'description' => '管理系统用户',
            'permissions' => ['user.manage'],
            'status' => 'active',
            'is_visible' => true,
            'sort_order' => 1,
        ]);

        // 角色管理子菜单
        Menu::create([
            'parent_id' => $systemMenu->id,
            'name' => '角色管理',
            'code' => 'role_manage',
            'icon' => 'shield',
            'path' => '/system/roles',
            'component' => 'RoleManage',
            'type' => 'page',
            'description' => '管理系统角色和权限',
            'permissions' => ['role.manage'],
            'status' => 'active',
            'is_visible' => true,
            'sort_order' => 2,
        ]);

        // 菜单管理子菜单
        Menu::create([
            'parent_id' => $systemMenu->id,
            'name' => '菜单管理',
            'code' => 'menu_manage',
            'icon' => 'menu',
            'path' => '/system/menus',
            'component' => 'MenuManage',
            'type' => 'page',
            'description' => '管理系统菜单',
            'permissions' => ['menu.manage'],
            'status' => 'active',
            'is_visible' => true,
            'sort_order' => 3,
        ]);

        // 系统日志子菜单
        Menu::create([
            'parent_id' => $systemMenu->id,
            'name' => '系统日志',
            'code' => 'system_log',
            'icon' => 'file-text',
            'path' => '/system/logs',
            'component' => 'SystemLog',
            'type' => 'page',
            'description' => '查看系统操作日志',
            'permissions' => ['system.log'],
            'status' => 'active',
            'is_visible' => true,
            'sort_order' => 4,
        ]);

        // 人事管理菜单
        $hrMenu = Menu::create([
            'name' => '人事管理',
            'code' => 'hr',
            'icon' => 'user-check',
            'path' => '/hr',
            'component' => 'HRLayout',
            'type' => 'menu',
            'description' => '人事管理相关功能',
            'permissions' => ['hr.manage'],
            'status' => 'active',
            'is_visible' => true,
            'sort_order' => 2,
        ]);

        // 员工管理子菜单
        Menu::create([
            'parent_id' => $hrMenu->id,
            'name' => '员工管理',
            'code' => 'employee_manage',
            'icon' => 'users',
            'path' => '/hr/employees',
            'component' => 'EmployeeManage',
            'type' => 'page',
            'description' => '管理员工信息',
            'permissions' => ['employee.manage'],
            'status' => 'active',
            'is_visible' => true,
            'sort_order' => 1,
        ]);

        // 部门管理子菜单
        Menu::create([
            'parent_id' => $hrMenu->id,
            'name' => '部门管理',
            'code' => 'department_manage',
            'icon' => 'building',
            'path' => '/hr/departments',
            'component' => 'DepartmentManage',
            'type' => 'page',
            'description' => '管理组织架构',
            'permissions' => ['department.manage'],
            'status' => 'active',
            'is_visible' => true,
            'sort_order' => 2,
        ]);

        // 岗位管理子菜单
        Menu::create([
            'parent_id' => $hrMenu->id,
            'name' => '岗位管理',
            'code' => 'position_manage',
            'icon' => 'briefcase',
            'path' => '/hr/positions',
            'component' => 'PositionManage',
            'type' => 'page',
            'description' => '管理岗位信息',
            'permissions' => ['position.manage'],
            'status' => 'active',
            'is_visible' => true,
            'sort_order' => 3,
        ]);

        // 个人中心菜单
        $profileMenu = Menu::create([
            'name' => '个人中心',
            'code' => 'profile',
            'icon' => 'user',
            'path' => '/profile',
            'component' => 'ProfileLayout',
            'type' => 'menu',
            'description' => '个人相关功能',
            'permissions' => ['profile.view'],
            'status' => 'active',
            'is_visible' => true,
            'sort_order' => 3,
        ]);

        // 个人信息子菜单
        Menu::create([
            'parent_id' => $profileMenu->id,
            'name' => '个人信息',
            'code' => 'profile_info',
            'icon' => 'user-circle',
            'path' => '/profile/info',
            'component' => 'ProfileInfo',
            'type' => 'page',
            'description' => '查看和编辑个人信息',
            'permissions' => ['profile.view'],
            'status' => 'active',
            'is_visible' => true,
            'sort_order' => 1,
        ]);

        // 修改密码子菜单
        Menu::create([
            'parent_id' => $profileMenu->id,
            'name' => '修改密码',
            'code' => 'change_password',
            'icon' => 'lock',
            'path' => '/profile/password',
            'component' => 'ChangePassword',
            'type' => 'page',
            'description' => '修改登录密码',
            'permissions' => ['profile.edit'],
            'status' => 'active',
            'is_visible' => true,
            'sort_order' => 2,
        ]);
    }

    /**
     * 创建根部门
     */
    private function createRootDepartment(): void
    {
        Department::create([
            'name' => '公司总部',
            'code' => 'HQ',
            'description' => '公司总部，最高层级部门',
            'status' => 'active',
            'sort_order' => 1,
        ]);
    }

    /**
     * 创建系统用户
     */
    private function createSystemUser(): void
    {
        // 创建超级管理员用户
        $adminUser = User::create([
            'username' => 'admin',
            'phone' => '13800000000',
            'password' => Hash::make('admin123'),
            'status' => 'active',
            'preferences' => [
                'language' => 'zh_CN',
                'theme' => 'light',
                'timezone' => 'Asia/Shanghai',
            ],
        ]);

        // 分配超级管理员角色
        $adminRole = Role::where('code', 'super_admin')->first();
        if ($adminRole) {
            $adminUser->roles()->attach($adminRole->id, [
                'start_date' => now(),
                'status' => 'active',
                'assigned_at' => now(),
                'reason' => '系统初始化',
            ]);
        }
    }
}
